﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            try
            {
                this.BackgroundImage = new Bitmap(@"Resources\background.jpg");
                this.Text = SqlHelper.readConfigFile("Application Name");
            }
            catch
            {
                this.BackgroundImage = null;
                Console.Write("Background not found!");
            }
            try
            {
                this.Closing += new System.ComponentModel.CancelEventHandler(this.CloseAppLication);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void CloseAppLication(Object sender, System.ComponentModel.CancelEventArgs e)
        {
            //loginBus.logOut(frmLogin.nhanVien.UserName.Trim());
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void danhSáchHọcViênToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CustomerList f = new CustomerList();
            f.MdiParent = this;
            f.Show();
        }

        private void thêmHọcViênMớiToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Purchase f = new Purchase();
            f.ShowDialog();
        }

        private void tổngTiềnThuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Report f = new Report();
            f.MdiParent = this;
            f.Show();
        }

        private void xemDanhSáchHọcViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReportDetail f = new ReportDetail();
            f.MdiParent = this;
            f.Show();
        }

        private void danhSáchHọcViênTậpTheoNgàyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CustomerDayOnly f = new CustomerDayOnly();
            f.MdiParent = this;
            f.Show();
        }

        private void thêmHọcViêmMớiTậpTheoNgàyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PurchaseDay f = new PurchaseDay();
            f.ShowDialog();
        }

        private void Main_Shown(object sender, EventArgs e)
        {
            string strA = ShareMethod.Read(UserConstant.RA);
            string strB = ShareMethod.Read(UserConstant.RB);
            string strC = ShareMethod.Read(UserConstant.RC);
            if (string.IsNullOrEmpty(strA)
                || string.IsNullOrEmpty(strB)
                || string.IsNullOrEmpty(strC)
                || strA.Length < 4
                || strB.Length < 5
                || strC.Length < 5
                || !CheckLicense.checkA(strA)
                || !CheckLicense.checkB(strB)
                || !CheckLicense.checkC(strC)
            )
            {

                EnterLicense en = new EnterLicense();
                en.ShowDialog();
                en.Dispose();
            }else
            {
                string year = "20" + strA.Substring(1, 2);
                string month = strB.Substring(3,2);
                string day = strC.Substring(3,2);
                string expireDate = year + month + day;
                if (!CheckLicense.checkDate(expireDate))
                {
                    EnterLicense en = new EnterLicense();
                    en.ShowDialog();
                    en.Dispose();
                }
                else
                {
                    DateTime expDate = CheckLicense.convertString2Date(expireDate);
                    if (DateTime.Now.CompareTo(expDate) > 0)
                    {
                        Error er = new Error("Hệ thống hết hạn sử dụng. Bạn vui lòng nhập mã License mới.");
                        er.ShowDialog();
                        er.Dispose();

                        EnterLicense en = new EnterLicense();
                        en.ShowDialog();
                        en.Dispose();
                    }
                }
            }
            Login f = new Login();
            f.ShowDialog();
            if (Login.userName == null || Login.userName.Equals(""))
            {
                this.Dispose();
            }
            else
            {
                ngườiDùngToolStripMenuItem.Text = UserConstant.USER + LoginDao.fullName;

                if (LoginDao.adminFlag)
                {
                    quảnTrịToolStripMenuItem.Visible = true;
                    addNewUserTt.Visible = true;
                    reportTt.Visible = true;
                    chiTiêuToolStripMenuItem.Visible = true;
                }
                else
                {
                    quảnTrịToolStripMenuItem.Visible = false;
                    addNewUserTt.Visible = false;
                    reportTt.Visible = false;
                    chiTiêuToolStripMenuItem.Visible = false;
                }
            }
            openCustomer();
        }
        private void openCustomer()
        {
            try
            {
                CustomerList cus = new CustomerList();
                cus.MdiParent = this;
                cus.Show();
                cus.WindowState = FormWindowState.Minimized;
                cus.WindowState = FormWindowState.Maximized;
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                Application.Exit();
            }
        }
        private void minimizeOnpeningForm()
        {
            List<Form> formsFromOtherAssemblies = new List<Form>();
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name != UserConstant.MAIN_FORM)
                {
                    formsFromOtherAssemblies.Add(form);
                }
            }
            for (int i = 0; i < formsFromOtherAssemblies.Count; i++)
            {
                formsFromOtherAssemblies[i].Close();
            }
        }

        private bool ActiveOpenForm(string formName)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name.Equals(formName))
                {
                    form.Activate();
                    return true;
                }
            }
            return false;
        }

        private void CloseOpenForm(string formName)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name.Equals(formName))
                {
                    form.Close();
                    break;
                }
            }
        }

        private void maximizeOnpeningForm()
        {
            List<Form> formsFromOtherAssemblies = new List<Form>();
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name != UserConstant.MAIN_FORM)
                {
                    formsFromOtherAssemblies.Add(form);
                }
            }
            for (int i = 0; i < formsFromOtherAssemblies.Count; i++)
            {
                formsFromOtherAssemblies[i].WindowState = FormWindowState.Maximized;
            }
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            minimizeOnpeningForm();
            ngườiDùngToolStripMenuItem.Text = UserConstant.USER;
            quảnTrịToolStripMenuItem.Visible = false;
            quảnTrịToolStripMenuItem.Visible = false;
            addNewUserTt.Visible = false;
            reportTt.Visible = false;
            chiTiêuToolStripMenuItem.Visible = false;
            Login f = new Login();
            f.ShowDialog();
            ngườiDùngToolStripMenuItem.Text = UserConstant.USER + LoginDao.fullName;
            if (LoginDao.adminFlag)
            {
                quảnTrịToolStripMenuItem.Visible = true;
                addNewUserTt.Visible = true;
                reportTt.Visible = true;
                chiTiêuToolStripMenuItem.Visible = true;
            }
            else
            {
                quảnTrịToolStripMenuItem.Visible = false;
                addNewUserTt.Visible = false;
                reportTt.Visible = false;
                chiTiêuToolStripMenuItem.Visible = false;
            }
        }

        private void ngườiDùngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangePassword f = new ChangePassword();
            f.MdiParent = this;
            f.Show();
        }

        private void thêmNgườiDùngMớiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewUser ne = new NewUser();
            ne.MdiParent = this;
            ne.Show();
        }

        private void chiTiêuToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Payment pay = new Payment();
            pay.MdiParent = this;
            pay.Show();
        }

        private void báoCáoChiTiêuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PaymentsList pay = new PaymentsList();
            pay.MdiParent = this;
            pay.Show();
        }

        private void báoCáoThuChiTiêuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetAndPayments pay = new GetAndPayments();
            pay.MdiParent = this;
            pay.Show();
        }

        private void thêmMớiHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddProducts ad = new AddProducts();
            ad.ShowDialog();
        }

        private void thêmMớiTàiSảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddAssets ad = new AddAssets();
            ad.ShowDialog();
        }

        private void danhMụcĐơnVịToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Unit unit = new Unit();
            unit.MdiParent = this;
            unit.Show();
        }

        private void danhMụcTàiSảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AssetList list = new AssetList();
            list.MdiParent = this;
            list.Show();
        }

        private void liệtKêTàiSảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RProperties list = new RProperties();
            list.MdiParent = this;
            list.Show();
        }

        private void oToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Unit unit = new Unit();
            unit.MdiParent = this;
            unit.Show();
        }

        private void danhMụcHàngHoánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProductList f = new ProductList();
            f.MdiParent = this;
            f.Show();

        }

        private void bánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FSale f = new FSale();
            f.MdiParent = this;
            f.Show();
        }

        private void báoCáoDoanhThuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Report f = new Report();
            f.MdiParent = this;
            f.Show();
        }

        private void báoCáoThChiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetAndPayments pay = new GetAndPayments();
            pay.MdiParent = this;
            pay.Show();
        }

        private void báoCáoChiTiêuToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PaymentsList pay = new PaymentsList();
            pay.MdiParent = this;
            pay.Show();
        }

        private void xemDanhSáchHọcViênToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ReportDetail f = new ReportDetail();
            f.MdiParent = this;
            f.Show();
        }

        private void logOutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            minimizeOnpeningForm();
            ngườiDùngToolStripMenuItem.Text = UserConstant.USER;
            quảnTrịToolStripMenuItem.Visible = false;
            Login f = new Login();
            f.ShowDialog();
            ngườiDùngToolStripMenuItem.Text = UserConstant.USER + LoginDao.fullName;
            if (LoginDao.adminFlag)
            {
                quảnTrịToolStripMenuItem.Visible = true;
                addNewUserTt.Visible = true;
                reportTt.Visible = true;
                chiTiêuToolStripMenuItem.Visible = true;
            }
            else
            {
                quảnTrịToolStripMenuItem.Visible = false;
                addNewUserTt.Visible = false;
                reportTt.Visible = false;
                chiTiêuToolStripMenuItem.Visible = false;
            }
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void thêmNgườiDùngMớiToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            NewUser ne = new NewUser();
            ne.MdiParent = this;
            ne.Show();
        }

        private void báoCáoDoanhThuTừBánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SalesWithDebitReport f = new SalesWithDebitReport();
            f.MdiParent = this;
            f.Show();
        }

        private void xemBáoCáoDanhSáchHọcViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReportDetail f = new ReportDetail();
            f.MdiParent = this;
            f.Show();
        }

        private void xemHọcViênĐếnHạnĐóngTiềnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WarningList f = new WarningList();
            f.MdiParent = this;
            f.Show();
        }

        private void baToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaleReport f = new SaleReport();
            f.MdiParent = this;
            f.Show();
        }

        private void báoCáoDanhThuTừBánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaleReportSum f = new SaleReportSum();
            f.MdiParent = this;
            f.Show();
        }

        private void muaHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Import f = new Import(1);
            f.MdiParent = this;
            f.Show();
        }

        private void nhậpĐiềuChỉnhHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Import f = new Import(2);
            f.MdiParent = this;
            f.Show();
        }

        private void danhSáchNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmImportList f = new FrmImportList();
            f.MdiParent = this;
            f.Show();
        }

        private void báoCáoNhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmImportReport f = new FrmImportReport();
            f.MdiParent = this;
            f.Show();
        }

        private void báoCáoNhậpBánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmImExreport f = new FrmImExreport();
            f.MdiParent = this;
            f.Show();
        }

        private void báoCáoTồnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frminventory f = new Frminventory();
            f.MdiParent = this;
            f.Show();
        }

        private void kếtSổToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmReset f = new FrmReset();
            f.ShowDialog();
            f.Dispose();
        }

        private void hiểnThịKiểuLướiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AllFormMenu f = new AllFormMenu();
            f.MdiParent = this;
            f.Show();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CloseOpenForm("CustomerList");
                CustomerList f = new CustomerList(txtSearch.Text);
                f.MdiParent = this;
                f.Show();
                txtSearch.Text = "Tìm nhanh";
                txtSearch.TextBoxTextAlign = HorizontalAlignment.Center;
            }            
        }

        private void txtSearch_Enter(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            txtSearch.TextBoxTextAlign = HorizontalAlignment.Left;
        }

        private void txtSearch_Leave(object sender, EventArgs e)
        {
            txtSearch.Text = "Tìm nhanh";
            txtSearch.TextBoxTextAlign = HorizontalAlignment.Center;
        }
    }
}
